#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>

int main()
{
    printf("我变成了一个进程: pid:%d\n", getpid());
    pid_t id = fork();
    if (id == 0)
    {
        printf("下面的代码是子进程在进行\n");
        sleep(1);

       // char *myargv[] = {
       //     "ls",
       //     "-a",
       //     "-l",
       //     NULL
       // };
       
        char *myargv[] = {
            "othercmd",
            "-a",
            "-b",
            "-c",
            NULL 
        };

        char *myenv[] = {
            "PATH=/home/lyf/lesson22/exec/cmd",
            NULL 
        };

        extern char **environ;
        putenv((char*)"hajimi=666"); 
        execvpe("othercmd", myargv, environ);
        //putenv((char*)"PATH=/home/lyf/lesson22/cmd");
        //if (execvpe("./cmd/othercmd", myargv, myenv) == -1)
        //    perror("execvpe");
        //execvp("ls", myargv);
        //execv("/usr/bin/ls", argv);
        //execlp("ls", "ls", "-a", "-l", NULL);
        //execl("/usr/bin/ls", "ls", "-a", "-l", NULL);
        exit(3);
    }


    int status = 0;
    pid_t rid = waitpid(id, &status, 0);
    if (rid > 0)
    {
        printf("wait success!, exit_code: %d\n", WEXITSTATUS(status));
    }

    return 0;
}
